package com.web.wechat.web.admin;

import com.blue.core.dict.HttpMethod;
import com.blue.http.annotation.Http;
import com.blue.http.message.HttpRequest;
import com.blue.http.message.View;
import com.blue.jdbc.Page;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.web.base.user.facade.AddressFacade;
import com.web.base.user.facade.UserFacade;
import com.web.base.user.message.*;
import com.web.core.util.Const;
import com.web.wechat.global.model.User;
import com.web.wechat.global.util.HttpContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;

/**
 * 个人信息
 *
 * @author zhengjin
 * @since 1.0 2018年06月28日
 */
@Controller("adminInfoController")
@Http(url = "/admin/info")
public class InfoController
{
	@Autowired
	private UserFacade userFacade;
	@Autowired
	private AddressFacade addressFacade;
	@Resource
	private HttpContext httpContext;

	public InfoController()
	{
	}

	@Http(url = "/index", method = HttpMethod.GET)
	public View index(HttpRequest request)
	{
		return View.createBuilder().setView("/admin/info/index")
				.put(Const.FTL_TITLE, "个人信息")
				.build();
	}

	@Http(url = "/edit", method = HttpMethod.GET)
	public View editGet(HttpRequest request)
	{
		return View.createBuilder().setView("/admin/info/edit")
				.put(Const.FTL_TITLE, "个人信息")
				.build();
	}

	@Http(url = "/edit", method = HttpMethod.POST)
	public void editPost(HttpRequest request)
	{
		User user = httpContext.getUser(request);
		UserRequest userRequest = request.getPostObject(UserRequest.class);
		userRequest.setId(user.getId());
		userRequest.setName(user.getName());
		userFacade.update(userRequest);
		user.setNickname(userRequest.getNickname());
		user.setEmail(userRequest.getEmail());
		httpContext.setUser(request, user);
	}

	@Http(url = "/login_log", method = HttpMethod.GET)
	public View loginLogGet(HttpRequest request)
	{
		User user = httpContext.getUser(request);
		UserLoginLogRequest userLoginLogRequest = new UserLoginLogRequest();
		userLoginLogRequest.setUserId(user.getId());
		Page<UserLoginLogResponse> page = userFacade.listLoginPage(userLoginLogRequest, Const.PAGE_START, Const.PAGE_SIZE);
		return View.createBuilder().setView("/admin/info/login_log")
				.put(Const.FTL_TITLE, "登录日志")
				.put("page", page)
				.build();
	}
	@Http(url = "/login_log", method = HttpMethod.POST)
	public View loginLogPost(HttpRequest request)
	{
		int start = request.getPostInt("start", 1);
		User user = httpContext.getUser(request);
		UserLoginLogRequest userLoginLogRequest = new UserLoginLogRequest();
		userLoginLogRequest.setUserId(user.getId());
		Page<UserLoginLogResponse> page = userFacade.listLoginPage(userLoginLogRequest, start, Const.PAGE_SIZE);
		return View.createBuilder().setView("/admin/info/login_log_grid")
				.put(Const.FTL_TITLE, "登录日志")
				.put("page", page)
				.build();
	}

	@Http(url = "/address", method = HttpMethod.GET)
	public View address(HttpRequest request)
	{
		User user = httpContext.getUser(request);
		List<AddressResponse> addressList = addressFacade.listByUser(user.getId(), null);
		return View.createBuilder().setView("/admin/info/address")
				.put(Const.FTL_TITLE, "收货地址")
				.put("addressList", addressList)
				.build();
	}
	@Http(url = "/address_get", method = HttpMethod.GET)
	public View addressGet(HttpRequest request)
	{
		int id = request.getQueryInt("id", 0);
		AddressResponse response = null;
		if (id > 0)
		{
			response = addressFacade.get(id);
		}
		return View.createBuilder().setView("/admin/info/address_get")
				.put(Const.FTL_TITLE, "收货地址")
				.put("address", response == null ? new AddressResponse() : response)
				.build();
	}
	@Http(url = "/address_save", method = HttpMethod.POST)
	public void addressSave(HttpRequest request)
	{
		User user = httpContext.getUser(request);
		AddressRequest addressRequest = request.getPostObject(AddressRequest.class);
		addressRequest.setUserId(user.getId());
		if (addressRequest.getId() == null)
		{
			ValidationUtil.valid(addressRequest, SaveModel.class);
			addressFacade.add(addressRequest);
		}
		else
		{
			ValidationUtil.valid(addressRequest, UpdateModel.class);
			addressFacade.update(addressRequest);
		}
	}

}
